package com.zzyy.study.controller;

import com.zzyy.study.entities.User;
import com.zzyy.study.exp.BusinessException;
import com.zzyy.study.resp.ResultData;
import com.zzyy.study.resp.ReturnCodeEnum;
import com.zzyy.study.service.UserService;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @auther zzyy
 * @create 2024-10-03 19:48
 */
@RestController
@Slf4j
public class UserController
{
    @Resource
    private UserService userService;

    // 添加
    @PostMapping("/user/add")
    public ResultData add(@RequestBody User user){
        // 1.非空效验
        if(Objects.isNull(user))
        {
            throw new BusinessException(ReturnCodeEnum.REQPARAM_ISNULL.getCode(), ReturnCodeEnum.REQPARAM_ISNULL.getMessage());
        }
        // 2.执行数据库的添加操作
        boolean result = userService.save(user);

        return ResultData.success(result);
    }
    // 查询
    @GetMapping("/user/list")
    public ResultData<List<User>> getList(){
        List<User> list = userService.list();
        return ResultData.success(list);
    }
}
